import React, { PureComponent } from 'react'
import { Table } from 'antd';

import EllipsisTooltip from '../EllipsisTooltip'
import { Conf } from '../../utils';

class Index extends PureComponent{
    state={
        tableconf:{
            scroll : Conf.scroll_Conf,
            pagination : Conf.page_Conf,
            ...Conf.table_Conf
        }
    }
    render(){
        let { tableconf } = this.state;
        return(
            <Table 
                {...tableconf}
            />
        );
    }

    static getDerivedStateFromProps(props, state){
        let { tableconf } = state;

        if(
            props.loading !== tableconf.loading
            || props.dataSource !== tableconf.dataSource
            || props.dataSource !== tableconf.dataSource
            || props.rowSelection !== tableconf.rowSelection
            || props.pagination !== undefined
            || props.scroll !== undefined
            || props.bordered !== undefined
        ){
            
            if(props.pagination){
                let pageconf = tableconf.pagination;
                pageconf = Object.assign(pageconf, props.pagination);
                //更新prop的pagination
                Object.assign(props.pagination, pageconf);
            }
            tableconf = Object.assign(tableconf, props);
            if(tableconf.hasOwnProperty('columns')){
                for (let i = 0; i < tableconf['columns'].length; i++) {
                    if(!tableconf['columns'][i].hasOwnProperty('render') && !tableconf['columns'][i].hasOwnProperty('tree')){
                        tableconf['columns'][i]['onCell']=()=>{
                            return {
                                style: {
                                    whiteSpace: 'nowrap',
                                    maxWidth: tableconf['columns'][i]['width']
                                }
                            };
                        }
                        tableconf['columns'][i].render=(text) => <EllipsisTooltip title={text}>{text}</EllipsisTooltip>
                    }
                }
            }
            return{
                tableconf
            }
        }
        return null;
    }
}

/**
 * ,
    onCell: () => {
      return {
        style: {
          whiteSpace: 'nowrap',
          maxWidth: 100,
        }
      }
    }

    rowSelection = {
        onChange: (selectedRowKeys, selectedRows) => {
        console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
        },
        onSelect: (record, selected, selectedRows) => {
        console.log(record, selected, selectedRows);
        },
        onSelectAll: (selected, selectedRows, changeRows) => {
        console.log(selected, selectedRows, changeRows);
        },
    };
 */
export default Index